/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import com.opensymphony.xwork2.ActionContext;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import models.AccountBO;
import myconfession.dao.AccountDAO;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

/**
 *
 * @author Liem
 */
public class GetFBAccessTokenAction {

    private String returnUrl;
    private String access_token;
    private String username;

    public GetFBAccessTokenAction() {
    }

    public String execute() throws Exception {


        HttpServletRequest request = ServletActionContext.getRequest();
        Map session = ServletActionContext.getContext().getSession();

        if (access_token == null) {
            if (session.get("USER") != null) {
                username = session.get("USER").toString();
                String scheme = request.getScheme();             // http
                String serverName = request.getServerName();     // hostname.com
                int serverPort = request.getServerPort();        // port
                returnUrl = scheme + "://" + serverName + ":" + serverPort + "/"
                        + request.getRequestURI();
                String fbApp = "http://myconfession.herokuapp.com/";
                returnUrl = fbApp + "?returnUrl=" + returnUrl + "&username=" + username;
                return "getToken";
            }


        }
        if (username != null) {
            if (AccountDAO.setFbToken(username, access_token)) {
                AccountBO.setFbToken(username, access_token);
                session.put("HASTOKEN", "true");
                session.put("USER", username);
            }
        }

        return "success";

    }

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
}
